package info.niwota.web.im;

import info.niwota.web.proxy.ProxyHandler;
import info.niwota.web.proxy.Uri;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import jvs.peer.io.ChannelManager;

import x.org.apache.http.HttpRequest;
import x.org.apache.http.HttpResponse;
import x.org.apache.http.HttpStatus;
import x.org.apache.http.entity.ContentProducer;
import x.org.apache.http.entity.EntityTemplate;
import x.org.apache.http.protocol.HttpContext;

public class Main {
	
	private static File getTop() {
		File home = new File(System.getProperty("user.home"));
		String user = System.getProperty("user.name");
		File top = new File(home, "." + user);
		
		return top;
	}
	
	private static class XmppProxyHandler extends ProxyHandler {
		
		protected boolean handleLocal(final Uri uri, final HttpContext context,
				final HttpRequest request, final HttpResponse response)
				throws Exception {
			final String host = uri.getHost();
			
			if (host != null && !host.equalsIgnoreCase("localhost")) {
				return false;
			}		
			boolean refresh = Boolean.parseBoolean(uri.getParameter("refresh"));
			
			//final List<RemoteDevice> devices = RemoteDeviceDiscovery.getDevices(refresh);
			final List<String> peers = new ArrayList<String>();
			peers.add("user1@niwota.info");
			peers.add("user2@niwota.info");
			
			if (peers.size() > 0) {

				response.setStatusCode(HttpStatus.SC_OK);
				EntityTemplate body = new EntityTemplate(new ContentProducer() {

					public void writeTo(final OutputStream outstream)
							throws IOException {
						OutputStreamWriter w = new OutputStreamWriter(
								outstream, "UTF-8");
						w.write("<html><body><h3>Bluetooth Device</h3>");
						//LocalDevice local = LocalDevice.getLocalDevice();
						w.write("<p>Local: ");
						w.write("");
						//w.write(" " + local.getBluetoothAddress());
						w.write("<br />");
						//w.write("discoverable: "+local.getDiscoverable());
						w.write("<br />");
						//w.write("class: "+local.getDeviceClass().toString());
						w.write("</p>");
						w.write("<ul>");

						for (String d : peers) {
							w.write("<li>");
							//w.write(d.getFriendlyName(false));
							w.write("&nbsp;");
							//final String addr = d.getBluetoothAddress();
							final String addr = d;
							w.write("<a target=\"_blank\" href=\"/?realm=xmpp&peer="
									+ addr + "\">" + addr + "</a>");
							w.write("&nbsp;");
							//w.write("authenticated: " + d.isAuthenticated());
							w.write("&nbsp;");
							//w.write("encrypted: " + d.isEncrypted());
							w.write("&nbsp;");
							//w.write("trusted: " + d.isTrustedDevice());
							w.write("</li>");
						}

						w.write("</ul></body></html>");
						w.flush();
					}

				});
				body.setContentType("text/html; charset=UTF-8");
				response.setEntity(body);
				return true;
			}
			
			response.setStatusCode(HttpStatus.SC_OK);
			EntityTemplate body = new EntityTemplate(new ContentProducer() {

				public void writeTo(final OutputStream outstream)
						throws IOException {
					OutputStreamWriter w = new OutputStreamWriter(
							outstream, "UTF-8");
					w.write("<html><body><h1>");
					w.write("No Bluetooth devices found");
					w.write("</h1></body></html>");
					w.flush();
				}

			});
			body.setContentType("text/html; charset=UTF-8");
			response.setEntity(body);
			return true;
		}
	}

	/**
	 * @param args
	 * @throws Exception 
	 */
	public static void main(String[] args) throws Exception {
		File top = getTop();
//		System.out.println(top);
//		init();
//		
//		final int port = 18080;
//		final XmppProxyHandler handler = new XmppProxyHandler();
//		
//		ProxyServer s = new ProxyServer(port, handler);
//		s.startup();
	}

	private static void init() throws Exception {
		final String network = "xmpp";
		Properties attrs = System.getProperties();
		attrs.put(ChannelManager.CHANNEL_DRIVER_CLASS,
				"jvs.peer.xmpp.Jabber");
		
		attrs.put("xmpp.host","localhost");
		attrs.put("xmpp.jid","user1@niwota.info");
		attrs.put("xmpp.port","5222");
		attrs.put("xmpp.username", "user1");
		attrs.put("xmpp.password", "test");
		attrs.put("xmpp.nickname", "client");
		attrs.put("xmpp.service", "");
		//attrs.put("xmpp.peer", "user2@niwota.info");
		
		ChannelManager.getInstance(network).init(attrs);
		ChannelManager.getInstance(network).startup();
	}

}
